package com.library.common.config;

import com.library.common.handler.CurrentUserMethodArgumentResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * 访问图片拦截和配置参数解析器
 *
 * @author: xyh
 * @create: 2023-09-17
 **/
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //前端url访问的路径，若有访问前缀，在访问时添加即可，这里不需添加。
        registry.addResourceHandler("/library/uploadFile/**")
                //映射的服务器存放图片目录。
                .addResourceLocations("file:/library/uploadFile/");
    }

    /**
     * HandlerMethodArgumentResolver和拦截器Interceptor一样，需要注册到IOC容器，
     * 可以通过webMvcConfig提供的addArgumentResolvers方法实现
     *
     * @param argumentResolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(currentUserMethodArgumentResolver());
    }

    @Bean
    public CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver(){
        return new CurrentUserMethodArgumentResolver();
    }

}
